package com.bj58.mis.apm.server.kylin.call;

import java.sql.Connection;

/**
 * @Author: wangxw
 * @Date: 2018/8/10 10:59
 */
public abstract class BaseCall {

    private int thread;
    private int loop;

    public BaseCall(String projectName, int thread, int loop){
        this.thread = thread;
        this.loop = loop;
        newThread(projectName);
    }

    public void loop(Connection conn) throws Exception{
        int i = 1;
        while(i <= loop) {
            query(conn);
            System.out.println("===============  " + i + "  " + Thread.currentThread().getName());
            i++;
        }
    }

    public void newThread(String projectName){
        int j = 1;
        while(j <= thread){
            new Thread(){
                @Override
                public void run() {
                    try {
                        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + Thread.currentThread().getName() + "is start");
                        loop(KylinJDBC.getConnection(projectName));
                        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" + Thread.currentThread().getName() + "is end");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }.start();
            j++;
        }
    }

    public abstract void query(Connection conn) throws Exception;
}
